package strcase

import (
	"strings"
)

func SnakeCase(s string) string {
	return delimiterCase(s, "_", false)
}

func UpperSnakeCase(s string) string {
	return delimiterCase(s, "_", true)
}

func delimiterCase(s string, delimiter string, upperCase bool) string {
	s = strings.TrimSpace(s)
	if len(s) == 0 {
		return s
	}
	var ns string
	sl := strings.Split(s, delimiter)

	for _, v := range sl {

		nsl := len(ns)

		if len(v) == 0 {
			continue
		}

		if nsl == 0 {
			if upperCase {
				ns = strings.ToUpper(v[:1]) + v[1:]
			} else {
				ns = v
			}
		}

		if nsl > 0 {
			ns += strings.ToUpper(v[:1]) + v[1:]
		}
	}
	return ns
}

// LowerFirstString 处理字符串，如果字符串不为空且开头两个字符是 C5，则去掉 C5 并将首字母小写
func LowerFirstString(s string) string {
	// 检查字符串是否为空
	if s == "" {
		return s
	}
	// 检查字符串开头两个字符是否为 C5
	if strings.HasPrefix(s, "C5") {
		s = s[2:]
	}
	// 将首字母小写
	if len(s) > 0 {
		s = strings.ToLower(string(s[0])) + s[1:]
	}
	return s
}
